<!DOCTYPE html>
<html lang="en">
<head><!-- hexo injector head_begin start -->
        <link rel="preconnect" href="https://blog.fantasyke.cn/picx-images-hosting" crossorigin=""><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/uploads/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/uploads/avatar.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="0qBBGdG7vYORyT5XLZRUqQZ_y5kKYhgyUwNqjSIImg0">
  <meta name="msvalidate.01" content="63C90D0EC3DA6C31FD109132B7BEF502">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Zen+Maru+Gothic:300,300italic,400,400italic,700,700italic%7CZCOOL+XiaoWei:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.fantasyke.cn","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.21.1","exturl":true,"sidebar":{"position":"right","width_expanded":350,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":null},"fold":{"enable":true,"height":500},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":true,"nav":{"gitalk":{"text":"gitalk","order":-1}},"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="💡在生产环境使用swarm集群的注意事项">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker-Swarm速成">
<meta property="og:url" content="https://blog.fantasyke.cn/posts/12327/index.html">
<meta property="og:site_name" content="Fantasy&#39;Ke Blog">
<meta property="og:description" content="💡在生产环境使用swarm集群的注意事项">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/docker/image.2krxyctvov.webp">
<meta property="article:published_time" content="2024-05-06T01:46:02.000Z">
<meta property="article:modified_time" content="2024-12-17T06:36:35.381Z">
<meta property="article:author" content="Fantasy-ke">
<meta property="article:tag" content="Docker">
<meta property="article:tag" content="Docker-Swarm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fantasy-ke.github.io/picx-images-hosting/docker/image.2krxyctvov.webp">


<link rel="canonical" href="https://blog.fantasyke.cn/posts/12327/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://blog.fantasyke.cn/posts/12327/","path":"posts/12327/","title":"Docker-Swarm速成"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Docker-Swarm速成 | Fantasy'Ke Blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Fantasy'Ke Blog" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Fantasy'Ke Blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="Search" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li><li class="menu-item menu-item-photos"><a href="/photos/" rel="section"><i class="fas fa-camera-retro fa-fw"></i>photos</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="Searching..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%BD%BF%E7%94%A8swarm%E9%9B%86%E7%BE%A4%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="nav-number">1.</span> <span class="nav-text"> 在生产环境使用swarm集群的注意事项</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#swarm%E9%9B%86%E7%BE%A4%E7%9A%84%E7%AE%A1%E7%90%86"><span class="nav-number">1.1.</span> <span class="nav-text"> swarm集群的管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#swarm%E9%9B%86%E7%BE%A4%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8-docker-componseyml-%E9%83%A8%E7%BD%B2%E5%AE%B9%E5%99%A8"><span class="nav-number">1.1.1.</span> <span class="nav-text"> swarm集群如何使用 docker-componse.yml 部署容器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E6%93%8D%E4%BD%9C"><span class="nav-number">1.1.2.</span> <span class="nav-text"> 如何操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-stack-%E5%91%BD%E4%BB%A4"><span class="nav-number">1.1.3.</span> <span class="nav-text"> docker stack 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E7%BC%96%E5%86%99%E6%94%AF%E6%8C%81-swarm-%E7%9A%84-docker-componseyml"><span class="nav-number">1.1.4.</span> <span class="nav-text"> 如何编写支持 swarm 的 docker-componse.yml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-stack-%E4%B8%8D%E6%94%AF%E6%8C%81%E7%9A%84docker-compose%E9%85%8D%E7%BD%AE"><span class="nav-number">1.1.5.</span> <span class="nav-text"> docker-stack 不支持的docker-compose配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8A%82%E7%82%B9%E7%AE%A1%E7%90%86"><span class="nav-number">1.2.</span> <span class="nav-text"> 节点管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84-manager-node"><span class="nav-number">1.2.1.</span> <span class="nav-text"> 创建一个新的 Manager Node</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B-swarm-%E5%BD%93%E5%89%8D%E7%8A%B6%E6%80%81"><span class="nav-number">1.2.2.</span> <span class="nav-text"> 查看 swarm 当前状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E8%8A%82%E7%82%B9%E5%88%97%E8%A1%A8"><span class="nav-number">1.2.3.</span> <span class="nav-text"> 查看节点列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E8%8A%82%E7%82%B9%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="nav-number">1.2.4.</span> <span class="nav-text"> 查看节点详细信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E6%96%B0%E7%9A%84-manager-node-%E5%88%B0%E9%9B%86%E7%BE%A4"><span class="nav-number">1.2.5.</span> <span class="nav-text"> 添加新的 Manager Node 到集群</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E6%96%B0%E7%9A%84-worker-node-%E5%88%B0%E9%9B%86%E7%BE%A4"><span class="nav-number">1.2.6.</span> <span class="nav-text"> 添加新的 Worker Node 到集群</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A6%81%E7%94%A8%E8%8A%82%E7%82%B9"><span class="nav-number">1.2.7.</span> <span class="nav-text"> 禁用节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E7%94%A8%E8%8A%82%E7%82%B9"><span class="nav-number">1.2.8.</span> <span class="nav-text"> 启用节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E8%8A%82%E7%82%B9"><span class="nav-number">1.2.9.</span> <span class="nav-text"> 更新节点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%AE%A1%E7%90%86"><span class="nav-number">1.3.</span> <span class="nav-text"> 服务管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E8%A1%8C%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.1.</span> <span class="nav-text"> 运行服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E8%BF%90%E8%A1%8C%E7%9A%84%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.2.</span> <span class="nav-text"> 查看运行的服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E6%9C%8D%E5%8A%A1%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="nav-number">1.3.3.</span> <span class="nav-text"> 查看服务的详细信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E6%9C%8D%E5%8A%A1%E8%BF%90%E8%A1%8C%E5%9C%A8%E9%82%A3%E4%BA%9B%E8%8A%82%E7%82%B9"><span class="nav-number">1.3.4.</span> <span class="nav-text"> 查看服务运行在那些节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BC%B8%E7%BC%A9%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.5.</span> <span class="nav-text"> 伸缩服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.6.</span> <span class="nav-text"> 删除服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%BB%9A%E5%8A%A8%E6%9B%B4%E6%96%B0%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.7.</span> <span class="nav-text"> 滚动更新服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.8.</span> <span class="nav-text"> 创建服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A3%80%E6%9F%A5%E6%9C%8D%E5%8A%A1%E7%8A%B6%E6%80%81"><span class="nav-number">1.3.9.</span> <span class="nav-text"> 检查服务状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.10.</span> <span class="nav-text"> 更新服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%E6%9A%82%E5%81%9C%E7%9A%84%E6%9C%8D%E5%8A%A1%E6%9B%B4%E6%96%B0"><span class="nav-number">1.3.11.</span> <span class="nav-text"> 重新启动暂停的服务更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%BB%9A%E5%8A%A8%E6%9B%B4%E6%96%B0"><span class="nav-number">1.3.12.</span> <span class="nav-text"> 查看服务的滚动更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B7%AF%E7%94%B1%E7%BD%91%E6%A0%BC"><span class="nav-number">1.3.13.</span> <span class="nav-text"> 路由网格</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%8E%9F%E7%90%86"><span class="nav-number">1.3.14.</span> <span class="nav-text"> 服务原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1%E6%97%B6%E5%85%AC%E5%BC%80%E7%AB%AF%E5%8F%A3"><span class="nav-number">1.3.15.</span> <span class="nav-text"> 创建服务时公开端口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E7%8E%B0%E6%9C%89%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%85%AC%E5%BC%80%E7%AB%AF%E5%8F%A3"><span class="nav-number">1.3.16.</span> <span class="nav-text"> 更新现有服务的公开端口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E6%9C%8D%E5%8A%A1%E5%8F%91%E5%B8%83%E7%9A%84%E7%AB%AF%E5%8F%A3"><span class="nav-number">1.3.17.</span> <span class="nav-text"> 查看服务发布的端口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AA%E5%85%AC%E5%BC%80tcp%E6%88%96udp%E7%AB%AF%E5%8F%A3"><span class="nav-number">1.3.18.</span> <span class="nav-text"> 只公开TCP或UDP端口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%85tcp"><span class="nav-number">1.3.19.</span> <span class="nav-text"> 仅TCP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%85udp"><span class="nav-number">1.3.20.</span> <span class="nav-text"> 仅UDP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#tcpudp"><span class="nav-number">1.3.21.</span> <span class="nav-text"> TCP+UDP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%95%E8%BF%87%E8%B7%AF%E7%94%B1%E7%BD%91%E6%A0%BC"><span class="nav-number">1.3.22.</span> <span class="nav-text"> 绕过路由网格</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86"><span class="nav-number">1.3.23.</span> <span class="nav-text"> 配置管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%99%AE%E9%80%9A%E9%85%8D%E7%BD%AE"><span class="nav-number">1.3.24.</span> <span class="nav-text"> 普通配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A0%E5%AF%86%E9%85%8D%E7%BD%AE"><span class="nav-number">1.3.25.</span> <span class="nav-text"> 加密配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%81%E5%AE%9A%E9%9B%86%E7%BE%A4"><span class="nav-number">1.3.26.</span> <span class="nav-text"> 锁定集群</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Fantasy-ke"
      src="/uploads/avatar.png">
  <p class="site-author-name" itemprop="name">Fantasy-ke</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;fantasy-ke"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly94LmNvbS9BX0RyZWFtZXJfSGFyZA==" title="Twitter → https:&#x2F;&#x2F;x.com&#x2F;A_Dreamer_Hard"><i class="fab fa-twitter fa-fw"></i>Twitter</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vQCVFNSU5MSVBOCVFNiVBMiVBNi16OWk=" title="YouTube → https:&#x2F;&#x2F;www.youtube.com&#x2F;@%E5%91%A8%E6%A2%A6-z9i"><i class="fab fa-youtube fa-fw"></i>YouTube</span>
      </span>
  </div>
<div class="cc-license animated" itemprop="sponsor">
  <a href="https://www.netlify.com" class="cc-opacity" title="Deploy with Netlify → https://www.netlify.com" target="_blank"><img width="80" src="https://www.netlify.com/img/global/badges/netlify-dark.svg" alt="Netlify"></a>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css">
<div id="aplayer" style="position:relative;left;0;bottom:0;"></div>
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
<script>
  const ap = new APlayer({
    container: document.getElementById('aplayer'),
    autoplay: false, //自动播放
    listFolded: true, //播放列表默认折叠
    listMaxHeight: 90, //播放列表最大高度
    order: 'list', //音频循环顺序, 可选值: 'list', 'random'
    loop: 'all', //音频循环播放, 可选值: 'all', 'one', 'none'
    theme: '#e9e9e9', //切换音频时的主题色，优先级低于audio.theme
    preload: 'none', //音频预加载，可选值: 'none', 'metadata', 'auto'
    mutex: true, //互斥，阻止多个播放器同时播放，当前播放器播放时暂停其他播放器
    lrcType: 3, //歌词格式，可选值：3（LRC文件歌词格式），1（JS字符串歌词格式）
    volume: 0.7, //默认音量，请注意播放器会记忆用户设置，用户手动设置音量后默认音量即失效
    fixed: false, //吸底模式（fixed:true），迷你模式（mini:true），普通模式（注释此行或者设置fixed:false）
    audio: [{
        name: '平凡之路',
        artist: '朴树',
        lrc: '/downloads/lrc/平凡之路-朴树.lrc',
        cover: 'http://p2.music.126.net/W_5XiCv3rGS1-J7EXpHSCQ==/18885211718782327.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C4000007nRJ74eQsF6.m4a?guid=942726817&vkey=64755357C5716983BEECE2981FEC3954F0DD03E7630EFF769D9C40C31F50D53E7D260CB63E22CE8F954B1144083716A1BABFC2093F8F1C14&uin=&fromtag=120032'
      },
      {
        name: '后会无期',
        artist: '徐良,汪苏泷',
        lrc: '/downloads/lrc/后会无期-G.E.M.邓紫棋.lrc',
        cover: 'http://p1.music.126.net/vpvPajo3kn88nHc7jUjeWQ==/5974746185758035.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C400004ECnYg0QNchK.m4a?guid=893251117&vkey=D9DCBB8C4A4CAF8550E97A2A574DE1231DFDD8BC0989CC724742C7B9831C4108E0A805A46E0F2B2A556228467C146B9C312E3E8B498614C7&uin=&fromtag=120032'
      }
    ]
  });

  // 实现切换音频时，根据音频的封面图片自适应主题色
  const colorThief = new ColorThief();
  const setTheme = (index) => {
    if (!ap.list.audios[index].theme) {
      colorThief.getColorAsync(ap.list.audios[index].cover, function(color) {
        ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
      });
    }
  };
  setTheme(ap.list.index);
  ap.on('listswitch', (data) => {
    setTheme(data.index);
  });
</script>



        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          Links
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">SunBlog</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">做梦的努力者</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0ZhbnRhc3ktS2U=" title="https:&#x2F;&#x2F;github.com&#x2F;Fantasy-Ke">github</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20vZmFudGFzeV9rZQ==" title="https:&#x2F;&#x2F;gitee.com&#x2F;fantasy_ke">gitee</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vZmFudGFzeS1rZQ==" title="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;fantasy-ke">我的园子</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8v" title="https:&#x2F;&#x2F;abp.io&#x2F;">abp</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8vZG9jcy9sYXRlc3Qv" title="https:&#x2F;&#x2F;abp.io&#x2F;docs&#x2F;latest&#x2F;">abp doc</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9ibG9ncy5tYXNhc3RhY2suY29tLw==" title="https:&#x2F;&#x2F;blogs.masastack.com&#x2F;">masa blog</span>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    Related Posts
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/61802/" rel="bookmark">
        <time class="popular-posts-time">2024-05-06</time>
        <br>
      Swarm-环境搭建
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/61442/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      7.Docker Compose
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/53249/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      1.Docker介绍
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/14184/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      2.Docker基本使用
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/31784/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      3.Docker基本命令
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://blog.fantasyke.cn/posts/12327/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.png">
      <meta itemprop="name" content="Fantasy-ke">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Docker-Swarm速成 | Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Docker-Swarm速成
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2024-05-06 09:46:02" itemprop="dateCreated datePublished" datetime="2024-05-06T09:46:02+08:00">2024-05-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2024-12-17 14:36:35" itemprop="dateModified" datetime="2024-12-17T14:36:35+08:00">2024-12-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/DevOps/" itemprop="url" rel="index"><span itemprop="name">DevOps</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="Word count in article">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">Word count in article: </span>
      <span>3k</span>
    </span>
    <span class="post-meta-item" title="Reading time">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">Reading time &asymp;</span>
      <span>11 mins.</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="在生产环境使用swarm集群的注意事项"><a class="markdownIt-Anchor" href="#在生产环境使用swarm集群的注意事项"></a> <strong>在生产环境使用swarm集群的注意事项</strong></h1>
<h2 id="swarm集群的管理"><a class="markdownIt-Anchor" href="#swarm集群的管理"></a> <strong>swarm集群的管理</strong></h2>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL2FkbWluX2d1aWRlLw==">https://docs.docker.com/engine/swarm/admin_guide/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="swarm集群如何使用-docker-componseyml-部署容器"><a class="markdownIt-Anchor" href="#swarm集群如何使用-docker-componseyml-部署容器"></a> <strong>swarm集群如何使用 docker-componse.yml 部署容器</strong></h3>
<h3 id="如何操作"><a class="markdownIt-Anchor" href="#如何操作"></a> <strong>如何操作</strong></h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL3N0YWNrLWRlcGxveS8=">https://docs.docker.com/engine/swarm/stack-deploy/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="docker-stack-命令"><a class="markdownIt-Anchor" href="#docker-stack-命令"></a> <strong>docker stack 命令</strong></h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3JlZmVyZW5jZS9jb21tYW5kbGluZS9zdGFjay8=">https://docs.docker.com/engine/reference/commandline/stack/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="如何编写支持-swarm-的-docker-componseyml"><a class="markdownIt-Anchor" href="#如何编写支持-swarm-的-docker-componseyml"></a> <strong>如何编写支持 swarm 的 docker-componse.yml</strong></h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLw==">https://docs.docker.com/compose/compose-file/compose-file-v3/<i class="fa fa-external-link-alt"></i></span></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">&quot;3.9&quot;</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">db:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">redis:3.0.5</span></span><br><span class="line">    <span class="attr">deploy:</span></span><br><span class="line">      <span class="attr">replicas:</span> <span class="number">6</span> <span class="comment"># 启动实例数量</span></span><br><span class="line">      <span class="attr">placement:</span> <span class="comment"># 配置容器实例位置------------</span></span><br><span class="line">        <span class="attr">max_replicas_per_node:</span> <span class="number">2</span> <span class="comment"># 每个节点最多运行容器实例数量</span></span><br><span class="line">        <span class="attr">constraints:</span> <span class="comment"># 将容器分配到匹配标签的节点运行</span></span><br><span class="line">          <span class="bullet">-</span> <span class="string">&quot;node.role==manager&quot;</span></span><br><span class="line">          <span class="bullet">-</span> <span class="string">&quot;engine.labels.operatingsystem==ubuntu 18.04&quot;</span></span><br><span class="line">        <span class="attr">preferences:</span> <span class="comment"># 将任务平均分配到不同类别的节点上</span></span><br><span class="line">          <span class="bullet">-</span> <span class="attr">spread:</span> <span class="string">node.labels.zone</span></span><br><span class="line">    <span class="attr">resources:</span> <span class="comment"># 资源限制------------</span></span><br><span class="line">        <span class="attr">limits:</span> <span class="comment"># 占用上限</span></span><br><span class="line">          <span class="attr">cpus:</span> <span class="string">&#x27;0.50&#x27;</span></span><br><span class="line">          <span class="attr">memory:</span> <span class="string">50M</span></span><br><span class="line">        <span class="attr">reservations:</span> <span class="comment"># 启动占用</span></span><br><span class="line">          <span class="attr">cpus:</span> <span class="string">&#x27;0.25&#x27;</span></span><br><span class="line">          <span class="attr">memory:</span> <span class="string">20M</span></span><br><span class="line">    <span class="attr">restart_policy:</span> <span class="comment"># 重启策略------------</span></span><br><span class="line">        <span class="attr">condition:</span> <span class="string">on-failure</span> <span class="comment"># 发生失败时</span></span><br><span class="line">                <span class="attr">delay:</span> <span class="string">5s</span> <span class="comment"># 重启时间间隔</span></span><br><span class="line">        <span class="attr">max_attempts:</span> <span class="number">3</span> <span class="comment">#最大尝试次数        </span></span><br><span class="line">                <span class="attr">window:</span> <span class="string">120s</span> <span class="comment"># 判断是否重启成功的等待时长                </span></span><br><span class="line">    <span class="attr">update_config:</span> <span class="comment"># 滚动更新的配置------------</span></span><br><span class="line">        <span class="attr">parallelism:</span> <span class="number">2</span> <span class="comment"># 同时更新的数量</span></span><br><span class="line">        <span class="attr">delay:</span> <span class="string">10s</span> <span class="comment"># 每次更新间隔时间</span></span><br><span class="line">        <span class="attr">monitor:</span> <span class="string">5s</span> <span class="comment"># 每次更新监控失败持续的时长</span></span><br><span class="line">        <span class="attr">failure_action:</span> <span class="string">&#x27;pause&#x27;</span> <span class="comment"># 滚动更新出现错误时执行的操作: continue/rollback/pause</span></span><br><span class="line">        <span class="attr">max_failure_ratio:</span> <span class="comment"># 允许的更新失败率</span></span><br><span class="line">        <span class="attr">order:</span> <span class="string">stop-first</span> <span class="comment"># 更新顺序 stop-first(旧任务在启动新任务之前停止)或start-first(新任务首先启动，运行中的任务会出现短暂重叠)</span></span><br><span class="line">    <span class="attr">rollback_config:</span> <span class="comment"># 更新失败如何回滚------------</span></span><br><span class="line">        <span class="attr">parallelism:</span> <span class="number">2</span> <span class="comment"># 每次回滚的数量，如果为0则全部回滚</span></span><br><span class="line">        <span class="attr">delay:</span> <span class="string">10s</span> <span class="comment"># 每次回滚间隔时间</span></span><br><span class="line">        <span class="attr">monitor:</span> <span class="string">5s</span> <span class="comment"># 每次更新监控失败持续的时长</span></span><br><span class="line">        <span class="attr">failure_action:</span> <span class="string">&#x27;pause&#x27;</span> <span class="comment"># 回滚出现错误时执行的操作: continue/rollback/pause</span></span><br><span class="line">        <span class="attr">max_failure_ratio:</span> <span class="comment"># 允许的回滚失败率，默认为0</span></span><br><span class="line">        <span class="attr">order:</span> <span class="string">stop-first</span> <span class="comment"># 回滚顺序 stop-first(旧任务在启动新任务之前停止)或start-first(新任务首先启动，运行中的任务会出现短暂重叠)</span></span><br></pre></td></tr></table></figure>
<h3 id="docker-stack-不支持的docker-compose配置"><a class="markdownIt-Anchor" href="#docker-stack-不支持的docker-compose配置"></a> <strong>docker-stack 不支持的docker-compose配置</strong></h3>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNidWlsZA==">build<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNjZ3JvdXBfcGFyZW50">cgroup_parent<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNjb250YWluZXJfbmFtZQ==">container_name<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNkZXZpY2Vz">devices<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyN0bXBmcw==">tmpfs<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNleHRlcm5hbF9saW5rcw==">external_links<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNsaW5rcw==">links<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNuZXR3b3JrX21vZGU=">network_mode<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNyZXN0YXJ0">restart<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNzZWN1cml0eV9vcHQ=">security_opt<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyN1c2VybnNfbW9kZQ==">userns_mode<i class="fa fa-external-link-alt"></i></span></li>
</ul>
<h2 id="节点管理"><a class="markdownIt-Anchor" href="#节点管理"></a> <strong>节点管理</strong></h2>
<blockquote>
<p>更多高级操作：</p>
</blockquote>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL2pvaW4tbm9kZXMv">给 Swarm 添加节点<i class="fa fa-external-link-alt"></i></span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL21hbmFnZS1ub2Rlcy8=">管理集群中的节点<i class="fa fa-external-link-alt"></i></span></li>
</ul>
<h3 id="创建一个新的-manager-node"><a class="markdownIt-Anchor" href="#创建一个新的-manager-node"></a> <strong>创建一个新的 Manager Node</strong></h3>
<p>使用</p>
<p><code>docker swarm init</code> 命令创建一个 Manager Node.</p>
<ul>
<li><code>-advertise-addr</code> 参数将 Manager Node 监听的IP设置为：</li>
</ul>
<p><code>192.168.99.100</code>.</p>
<blockquote>
<p>注意，使用</p>
</blockquote>
<ul>
<li><code>-advertise-addr</code> 默认监听的端口为</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2377</span></span><br></pre></td></tr></table></figure>
<p>swarm 中的其他 Node 必须能访问 Manager Node 的 IP.</p>
<p>输出包括将新节点加入 swarm 的命令。根据</p>
<ul>
<li><code>-token</code> 标志的值，节点将作为Master或Worker加入。</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker swarm init <span class="literal">--advertise-addr</span> ManagerIP地址</span><br></pre></td></tr></table></figure>
<p>例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu<span class="comment"># docker swarm init --advertise-addr 192.168.1.80</span></span><br><span class="line">Swarm initialized: current node (<span class="number">9</span>nxycsrinzgh5bgqwfus2ia8<span class="built_in">r</span>) is now a manager.</span><br><span class="line"></span><br><span class="line">To add a worker to this swarm, run the following command:</span><br><span class="line"></span><br><span class="line"> docker swarm join <span class="literal">--token</span> SWMTKN<span class="literal">-1-5qv7t73fvawvh795ckh3nxl9vnyo2hwwsqnnjwqyav3spj7ufu-1i7wir7oc3g9fh7yidg19i8p5</span> <span class="number">192.168</span>.<span class="number">1.80</span>:<span class="number">2377</span></span><br><span class="line"></span><br><span class="line">To add a manager to this swarm, run <span class="string">&#x27;docker swarm join-token manager&#x27;</span> and follow the instructions.</span><br></pre></td></tr></table></figure>
<p><code>docker swarm join-token manager</code> 命令用于获取添加新的 Manager Node 的命令参数</p>
<p><code>docker swarm join-token worker</code> 命令用于获取添加新的 Worker Node 的命令参数</p>
<blockquote>
<p>在生产环境 Manager Node 不推荐运行任何容器实例，但是 Swarm 调度器会分配给Manager Node，</p>
</blockquote>
<blockquote>
<p>因此我们可以通过 禁用节点 告诉 Swarm 调度器不要分配给 Manager Node 任何容器实例。</p>
</blockquote>
<h3 id="查看-swarm-当前状态"><a class="markdownIt-Anchor" href="#查看-swarm-当前状态"></a> <strong>查看 swarm 当前状态</strong></h3>
<p>docker info</p>
<p>例：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu# docker info</span><br><span class="line">Client:</span><br><span class="line">Context: default</span><br><span class="line">Debug Mode: false</span><br><span class="line">Plugins:</span><br><span class="line">app: Docker App (Docker Inc., v0.9.1-beta3)</span><br><span class="line">buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)</span><br><span class="line">scan: Docker Scan (Docker Inc., v0.9.0)</span><br><span class="line">Server:</span><br><span class="line">Swarm: active</span><br><span class="line">NodeID: 9nxycsrinzgh5bgqwfus2ia8r</span><br><span class="line">Is Manager: true</span><br><span class="line">ClusterID: lu8frg6np6dmqf3bjr7jyl628</span><br><span class="line">Managers: 1</span><br><span class="line">Nodes: 1</span><br><span class="line">Default Address Pool: 10.0.0.0/8</span><br><span class="line">SubnetSize: 24</span><br><span class="line">Data Path Port: 4789</span><br><span class="line">Orchestration:</span><br><span class="line">Task History Retention Limit: 5</span><br><span class="line">Raft:</span><br><span class="line">Snapshot Interval: 10000</span><br><span class="line">Number of Old Snapshots to Retain: 0</span><br><span class="line">Heartbeat Tick: 1</span><br><span class="line">Election Tick: 10</span><br><span class="line">Dispatcher:</span><br><span class="line">Heartbeat Period: 5 seconds</span><br><span class="line">CA Configuration:</span><br><span class="line">Expiry Duration: 3 months</span><br><span class="line">Force Rotate: 0</span><br><span class="line">Autolock Managers: false</span><br><span class="line">Root Rotation In Progress: false</span><br><span class="line">Node Address: 192.168.1.80</span><br><span class="line">Manager Addresses:</span><br><span class="line">192.168.1.80:2377</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="查看节点列表"><a class="markdownIt-Anchor" href="#查看节点列表"></a> <strong>查看节点列表</strong></h3>
<p>使用以下命令查看节点列表，节点</p>
<p><code>ID</code> 旁边的 <code>*</code> 表示你当前已经连接到此节点</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker node <span class="built_in">ls</span></span><br></pre></td></tr></table></figure>
<p>例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu<span class="comment"># docker node ls</span></span><br><span class="line">ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION</span><br><span class="line"><span class="number">9</span>nxycsrinzgh5bgqwfus2ia8<span class="built_in">r</span> *   u2004      Ready     Active         Leader           <span class="number">20.10</span>.<span class="number">11</span></span><br></pre></td></tr></table></figure>
<h3 id="查看节点详细信息"><a class="markdownIt-Anchor" href="#查看节点详细信息"></a> <strong>查看节点详细信息</strong></h3>
<p>使用以下命令查看节点的详情：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker node inspect <span class="literal">--pretty</span> 节点名称</span><br></pre></td></tr></table></figure>
<h3 id="添加新的-manager-node-到集群"><a class="markdownIt-Anchor" href="#添加新的-manager-node-到集群"></a> <strong>添加新的 Manager Node 到集群</strong></h3>
<p>在另一台机器运行 <code>docker swarm join</code> 命令加入已存在的集群中</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu<span class="comment"># docker swarm join-token managerTo add a manager to this swarm, run the following command:    docker swarm join --token SWMTKN-1-5qv7t73fvawvh795ckh3nxl9vnyo2hwwsqnnjwqyav3spj7ufu-ca1v0jiu9xwb6o20dqqajl4n1 192.168.1.80:2377</span></span><br></pre></td></tr></table></figure>
<p>Manager Node 必须是单数（Raft），生产环境推荐3台或5台作为 Manager Node</p>
<blockquote>
<p>docker swarm join-token manager 命令用于获取添加新的 Manager Node 的命令参数</p>
</blockquote>
<h3 id="添加新的-worker-node-到集群"><a class="markdownIt-Anchor" href="#添加新的-worker-node-到集群"></a> <strong>添加新的 Worker Node 到集群</strong></h3>
<p>在另一台机器运行</p>
<p><code>docker swarm join</code> 命令加入已存在的集群中</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker swarm join <span class="literal">--token</span> SWMTKN<span class="literal">-1-5qv7t73fvawvh795ckh3nxl9vnyo2hwwsqnnjwqyav3spj7ufu-1i7wir7oc3g9fh7yidg19i8p5</span> <span class="number">192.168</span>.<span class="number">1.80</span>:<span class="number">2377</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>docker swarm join-token worker 命令用于获取添加新的 Worker Node 的命令参数</p>
</blockquote>
<h3 id="禁用节点"><a class="markdownIt-Anchor" href="#禁用节点"></a> <strong>禁用节点</strong></h3>
<p>在某些情况下我们想要将某个节点禁用或将节点中运行的容器清空，使用以下命令即可实现：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker node update <span class="literal">--availability</span> drain 节点名称</span><br></pre></td></tr></table></figure>
<blockquote>
<p>被禁用的节点仍然存在集群中，只是不会被swarm调度运行容器实例</p>
</blockquote>
<h3 id="启用节点"><a class="markdownIt-Anchor" href="#启用节点"></a> <strong>启用节点</strong></h3>
<p>禁用节点后使用以下命令即可启用节点：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker node update <span class="literal">--availability</span> active 节点名称</span><br></pre></td></tr></table></figure>
<h3 id="更新节点"><a class="markdownIt-Anchor" href="#更新节点"></a> <strong>更新节点</strong></h3>
<p>更新节点</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker node update <span class="literal">--label-add</span> foo <span class="literal">--label-add</span> bar=baz 节点名称</span><br></pre></td></tr></table></figure>
<h2 id="服务管理"><a class="markdownIt-Anchor" href="#服务管理"></a> <strong>服务管理</strong></h2>
<blockquote>
<p>更多高级操作：</p>
</blockquote>
<blockquote>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3JlZmVyZW5jZS9jb21tYW5kbGluZS9zZXJ2aWNlLw==">https://docs.docker.com/engine/reference/commandline/service/<i class="fa fa-external-link-alt"></i></span></p>
</blockquote>
<blockquote>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNwbGFjZW1lbnQ=">https://docs.docker.com/compose/compose-file/compose-file-v3/#placement<i class="fa fa-external-link-alt"></i></span></p>
</blockquote>
<h3 id="运行服务"><a class="markdownIt-Anchor" href="#运行服务"></a> <strong>运行服务</strong></h3>
<p>连接到 Manager Node，使用</p>
<p><code>docker service create</code> 命令创建服务.</p>
<p>例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--replicas</span> <span class="number">1</span> <span class="literal">--name</span> helloworld alpine ping docker.com</span><br></pre></td></tr></table></figure>
<ul>
<li><code>-name</code> 指定服务名称为 <code>helloworld</code></li>
<li><code>-replicas</code> 指定服务运行实例数量为 <code>1</code></li>
<li>参数 <code>alpine</code> 表示运行的镜像为 <code>Alpine Linux</code></li>
<li>参数 <code>ping docker.com</code> 表示在容器中执行的命令</li>
</ul>
<h3 id="查看运行的服务"><a class="markdownIt-Anchor" href="#查看运行的服务"></a> <strong>查看运行的服务</strong></h3>
<p>在 Manager Node 运行此命令查看正在运行的服务列表：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service <span class="built_in">ls</span></span><br></pre></td></tr></table></figure>
<p>例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu<span class="comment"># docker service ls</span></span><br><span class="line">ID             NAME         MODE         REPLICAS   IMAGE           PORTS</span><br><span class="line">jkj80hnr73rn   helloworld   replicated   <span class="number">1</span>/<span class="number">1</span>        alpine:latest</span><br></pre></td></tr></table></figure>
<h3 id="查看服务的详细信息"><a class="markdownIt-Anchor" href="#查看服务的详细信息"></a> <strong>查看服务的详细信息</strong></h3>
<p>在 Manager Node 运行此命令查看服务的运行详情：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service inspect <span class="literal">--pretty</span> 服务名称</span><br></pre></td></tr></table></figure>
<blockquote>
<p>参数 --pretty 表示返回格式化后的详细信息，不加这个参数则打印 JSON 格式的信息</p>
</blockquote>
<h3 id="查看服务运行在那些节点"><a class="markdownIt-Anchor" href="#查看服务运行在那些节点"></a> <strong>查看服务运行在那些节点</strong></h3>
<p>在 Manager Node 使用此命令查看服务都在那些节点运行：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service <span class="built_in">ps</span> 服务名称</span><br></pre></td></tr></table></figure>
<blockquote>
<p>docker-swarm 中的服务实例由 swarm 调度。因此有部分服务的实例运行在 Manager Node 是正常表现。</p>
</blockquote>
<h3 id="伸缩服务"><a class="markdownIt-Anchor" href="#伸缩服务"></a> <strong>伸缩服务</strong></h3>
<p>docker-swarm 支持对服务实例进行动态伸缩，使用以下命令即可实现：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service scale 服务名称=实例数量(最少为<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<p>例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">root@u2004:/home/ubuntu<span class="comment"># docker service scale helloworld=2</span></span><br><span class="line">helloworld scaled to <span class="number">2</span></span><br><span class="line">overall progress: <span class="number">2</span> out of <span class="number">2</span> tasks</span><br><span class="line"><span class="number">1</span>/<span class="number">2</span>: running   [==================================================&gt;]</span><br><span class="line"><span class="number">2</span>/<span class="number">2</span>: running   [==================================================&gt;]</span><br><span class="line">verify: Service converged</span><br></pre></td></tr></table></figure>
<h3 id="删除服务"><a class="markdownIt-Anchor" href="#删除服务"></a> <strong>删除服务</strong></h3>
<p>在 Manager Node 使用以下命令删除服务：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service <span class="built_in">rm</span> 服务名称</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意，因为是集群的原因，集群中的Node将会存在延迟的情况，想确认服务是否被删除成功请使用 docker service ls 查看</p>
</blockquote>
<h3 id="滚动更新服务"><a class="markdownIt-Anchor" href="#滚动更新服务"></a> <strong>滚动更新服务</strong></h3>
<h3 id="创建服务"><a class="markdownIt-Anchor" href="#创建服务"></a> <strong>创建服务</strong></h3>
<p>进入 Manager Node 创建一个redis服务用于演示滚动更新：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker service create \</span><br><span class="line">  <span class="literal">--replicas</span> <span class="number">3</span> \</span><br><span class="line">  <span class="literal">--name</span> redis \</span><br><span class="line">  <span class="literal">--update-delay</span> <span class="number">10</span>s \</span><br><span class="line">  redis:<span class="number">3.0</span>.<span class="number">6</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>–update-dely 表示更新服务或服务集之间的时间延迟：1h10m3s，表示延迟1小时10分钟3秒。</p>
</blockquote>
<blockquote>
<p>调度器默认一次更新一个任务，可以通过 --update-parallelism 参数配置调度器同时更新服务数量。</p>
</blockquote>
<blockquote>
<p>默认情况下，当单个服务更新返回状态为 RUNNING，调度器会让另一个服务更新，直到所有服务都更新完成。</p>
</blockquote>
<blockquote>
<p>如果在更新期间某个服务返回 FAILED ，调度器会暂停更新，可以通过 --update-failure-action 参数配置控制当服务更新发生错误时的行为。</p>
</blockquote>
<h3 id="检查服务状态"><a class="markdownIt-Anchor" href="#检查服务状态"></a> <strong>检查服务状态</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service inspect <span class="literal">--pretty</span> redis</span><br></pre></td></tr></table></figure>
<h3 id="更新服务"><a class="markdownIt-Anchor" href="#更新服务"></a> <strong>更新服务</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service update <span class="literal">--image</span> redis:<span class="number">3.0</span>.<span class="number">7</span> redis</span><br></pre></td></tr></table></figure>
<blockquote>
<p>默认情况下，调度器将按以下方式更新服务：</p>
</blockquote>
<ul>
<li>停止一个服务</li>
<li>更新已停止的服务</li>
<li>启动已更新的服务</li>
</ul>
<blockquote>
<p>如果更新的服务返回 RUNNING ，等待指定的延迟时间后开始更新下一个服务</p>
</blockquote>
<blockquote>
<p>如果更新期间某个服务返回 FAILED ，则暂停服务更新</p>
</blockquote>
<h3 id="重新启动暂停的服务更新"><a class="markdownIt-Anchor" href="#重新启动暂停的服务更新"></a> <strong>重新启动暂停的服务更新</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service update redis</span><br></pre></td></tr></table></figure>
<blockquote>
<p>为了避免重复某些失败的更新，可以重新指定更新参数</p>
</blockquote>
<h3 id="查看服务的滚动更新"><a class="markdownIt-Anchor" href="#查看服务的滚动更新"></a> <strong>查看服务的滚动更新</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service <span class="built_in">ps</span> redis</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在swarm更新完成所有服务之前，你可以看到一些服务的镜像为 redis:3.0.6，另一些为 redis:3.0.7</p>
</blockquote>
<h3 id="路由网格"><a class="markdownIt-Anchor" href="#路由网格"></a> <strong>路由网格</strong></h3>
<p>docker swarm支持路由网格。路由网格让处于swarm集群中的任意一个节点都可以作为被访问的入口，即使此节点没有运行任何服务。</p>
<p>要在 swarm 集群中使用使用路由网格，首先需要开启加入swarm集群的节点的以下端口：</p>
<ul>
<li><code>7946</code> ：容器网络发现</li>
<li><code>4789</code> ：容器网络入口</li>
</ul>
<p>其次需要将节点服务实例的端口公开，使服务可以被外部访问（例如使用nginx做负载均衡）</p>
<h3 id="服务原理"><a class="markdownIt-Anchor" href="#服务原理"></a> <strong>服务原理</strong></h3>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/docker/image.2krxyctvov.webp" alt="1.2rv5sbkjzm.png" /></p>
<h3 id="创建服务时公开端口"><a class="markdownIt-Anchor" href="#创建服务时公开端口"></a> <strong>创建服务时公开端口</strong></h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker service create \</span><br><span class="line">  --name &lt;SERVICE-NAME&gt; \</span><br><span class="line">  --publish published=&lt;PUBLISHED-PORT&gt;,target=&lt;CONTAINER-PORT&gt; \</span><br><span class="line">  &lt;IMAGE&gt;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>–publish 与 -p 效果相同，其中 --published 值为公布的端口，target 值为容器内部监听的端口。–publish 的写法</p>
</blockquote>
<h3 id="更新现有服务的公开端口"><a class="markdownIt-Anchor" href="#更新现有服务的公开端口"></a> <strong>更新现有服务的公开端口</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker service update \</span><br><span class="line">  <span class="literal">--publish-add</span> published=&lt;PUBLISHED<span class="literal">-PORT</span>&gt;,target=&lt;CONTAINER<span class="literal">-PORT</span>&gt; \</span><br><span class="line">  &lt;SERVICE&gt;</span><br></pre></td></tr></table></figure>
<h3 id="查看服务发布的端口"><a class="markdownIt-Anchor" href="#查看服务发布的端口"></a> <strong>查看服务发布的端口</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker service inspect <span class="literal">--format</span>=<span class="string">&quot;&#123;&#123;json .Endpoint.Spec.Ports&#125;&#125;&quot;</span> 服务名称</span><br></pre></td></tr></table></figure>
<h3 id="只公开tcp或udp端口"><a class="markdownIt-Anchor" href="#只公开tcp或udp端口"></a> <strong>只公开TCP或UDP端口</strong></h3>
<p>默认情况下公开端口都是 TCP 端口，你可以通过参数配置公开端口的类型：</p>
<h3 id="仅tcp"><a class="markdownIt-Anchor" href="#仅tcp"></a> <strong>仅TCP</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">--publish</span> published=<span class="number">53</span>,target=<span class="number">53</span> \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<p>或</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">-p</span> <span class="number">53</span>:<span class="number">53</span> \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<h3 id="仅udp"><a class="markdownIt-Anchor" href="#仅udp"></a> <strong>仅UDP</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">--publish</span> published=<span class="number">53</span>,target=<span class="number">53</span>,protocol=udp \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<p>或</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">-p</span> <span class="number">53</span>:<span class="number">53</span>/udp \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<h3 id="tcpudp"><a class="markdownIt-Anchor" href="#tcpudp"></a> <strong>TCP+UDP</strong></h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">--publish</span> published=<span class="number">53</span>,target=<span class="number">53</span> \</span><br><span class="line">  <span class="literal">--publish</span> published=<span class="number">53</span>,target=<span class="number">53</span>,protocol=udp \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<p>或</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">-p</span> <span class="number">53</span>:<span class="number">53</span> \</span><br><span class="line">  <span class="literal">-p</span> <span class="number">53</span>:<span class="number">53</span>/udp \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<h3 id="绕过路由网格"><a class="markdownIt-Anchor" href="#绕过路由网格"></a> <strong>绕过路由网格</strong></h3>
<p>要绕过 swarm 集群的路由网格，需要使用</p>
<ul>
<li><code>-publish</code> 参数设置 <code>mode</code> 值为<code>host</code>。</li>
</ul>
<p>下面的命令使用</p>
<p><code>host</code> 模式创建全局服务并绕过路由网格：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker service create <span class="literal">--name</span> dns<span class="literal">-cache</span> \</span><br><span class="line">  <span class="literal">--publish</span> published=<span class="number">53</span>,target=<span class="number">53</span>,protocol=udp,mode=host \</span><br><span class="line">  <span class="literal">--mode</span> global \</span><br><span class="line">  dns<span class="literal">-cache</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>绕过路由网格后的注意事项：</p>
</blockquote>
<ul>
<li>如果你访问未运行服务的节点，则无法访问此服务</li>
<li>如果你希望在每个节点运行多个服务，就不能指定静态的端口。要么就允许docker随机分配一个公开端口（通过置空 <code>published</code> 参数的值实现）</li>
</ul>
<h3 id="配置管理"><a class="markdownIt-Anchor" href="#配置管理"></a> <strong>配置管理</strong></h3>
<h3 id="普通配置"><a class="markdownIt-Anchor" href="#普通配置"></a> <strong>普通配置</strong></h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL2NvbmZpZ3Mv">https://docs.docker.com/engine/swarm/configs/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="加密配置"><a class="markdownIt-Anchor" href="#加密配置"></a> <strong>加密配置</strong></h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL3NlY3JldHMv">https://docs.docker.com/engine/swarm/secrets/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="锁定集群"><a class="markdownIt-Anchor" href="#锁定集群"></a> <strong>锁定集群</strong></h3>
<blockquote>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL3N3YXJtX21hbmFnZXJfbG9ja2luZy8=">https://docs.docker.com/engine/swarm/swarm_manager_locking/<i class="fa fa-external-link-alt"></i></span></p>
</blockquote>
<p>默认情况下，swarm manager 使用的 Raft 日志在磁盘上是加密的。这种静态加密保护您的服务配置和数据免受攻击者访问加密的 Raft 日志。引入此功能的原因之一是支持<span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL3NlY3JldHMv">Docker 机密<i class="fa fa-external-link-alt"></i></span>功能。</p>
<p>当 Docker 重新启动时，用于加密 swarm 节点之间通信的 TLS 密钥和用于加密和解密磁盘上的 Raft 日志的密钥都被加载到每个管理器节点的内存中。Docker 有能力保护双向 TLS 加密密钥和用于加密和解密 Raft 静态日志的密钥，允许您拥有这些密钥并要求手动解锁您的管理器。此功能称为<em>自动锁定</em>。</p>
<p>当 Docker 重新启动时，您必须先 <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3N3YXJtL3N3YXJtX21hbmFnZXJfbG9ja2luZy8jdW5sb2NrLWEtc3dhcm0=">解锁 swarm<i class="fa fa-external-link-alt"></i></span>，使用 Docker 在 swarm 被锁定时生成的*密钥加密密钥。*您可以随时轮换此密钥加密密钥。</p>
<blockquote>
<p>注意：当新节点加入 swarm 时，您不需要解锁 swarm，因为密钥通过双向 TLS 传播给它。</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">







<div class="license">
  <div class="license-title">Docker-Swarm速成</div>
  <div class="license-link">
    <a href="https://blog.fantasyke.cn/posts/12327/">https://blog.fantasyke.cn/posts/12327/</a>
  </div>
  <div class="license-meta">
    <div class="license-meta-item">
      <div class="license-meta-title">本文作者</div>
      <div class="license-meta-text">
          Fantasy-ke
      </div>
    </div>
      <div class="license-meta-item">
        <div class="license-meta-title">发布于</div>
        <div class="license-meta-text">
          2024-05-06
        </div>
      </div>
      <div class="license-meta-item">
        <div class="license-meta-title">更新于</div>
        <div class="license-meta-text">
          2024-12-17
        </div>
      </div>
    <div class="license-meta-item">
      <div class="license-meta-title">许可协议</div>
      <div class="license-meta-text">
          禁止转载引用
      </div>
    </div>
  </div>
  <div class="license-statement">
      如需转载或引用本文，请先获得作者授权！
  </div>
</div>
          <div class="reward-container">
  <div>Buy me a coffee</div>
  <button>
    Donate
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Fantasy-ke WeChat Pay">
        <span>WeChat Pay</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>Welcome to my other publishing channels</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://github.com/fantasy-ke">
            <span class="icon">
              <i class="fab fa-github"></i>
            </span>

            <span class="label">GitHub</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Docker/" rel="tag"><i class="fa fa-tag"></i> Docker</a>
              <a href="/tags/Docker-Swarm/" rel="tag"><i class="fa fa-tag"></i> Docker-Swarm</a>
          </div>

        
  <div class="social-like a2a_kit a2a_kit_size_32 a2a_default_style">
    <a class="a2a_dd" target="_blank" rel="noopener" href="https://www.addtoany.com/share"></a>
      <a class="a2a_button_facebook"></a>
      <a class="a2a_button_twitter"></a>
      <a class="a2a_button_telegram"></a>
      <a class="a2a_button_wechat"></a>
  </div>

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/61802/" rel="prev" title="Swarm-环境搭建">
                  <i class="fa fa-angle-left"></i> Swarm-环境搭建
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/61401/" rel="next" title="Windows安装MongoDB">
                  Windows安装MongoDB <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2021 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-solid fa-t-rex"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Fantasy-ke</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>Word count total: </span>
    <span title="Word count total">72k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>Reading time total &asymp;</span>
    <span title="Reading time total">4:21</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div> 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  var now = new Date();
  function timer() {
    var grt= new Date("03/12/2022 13:14:21");//此处修改你的建站时间或者网站上线时间
    now.setTime(now.getTime()+250);
    days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
    hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
    if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
    seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
    var ages = `${dnum}天${hnum}小时${mnum}分${snum}秒`;
    div.innerHTML = `本站已安全运行 ${ages} 🐶`;
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".busuanzi-count");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>


<div class="footer-menu">
    <a style="color: chocolate;" href="/tags/">文章标签</a> · <a style="color: chocolate;"  href="/policy/">网站政策</a> · <a style="color: chocolate;"  href="/photos/">文章相册</a>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.umd.js" integrity="sha256-a+H7FYzJv6oU2hfsfDGM2Ohw/cR9v+hPfxHCLdmCrE8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>


  <script src="/js/third-party/addtoany.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"fantasy-ke","repo":"fantasy-ke.github.io","client_id":"Ov23li8eCFrhnzfiGx5T","client_secret":"ce23581c2059392ebd00afd08d5ba43f767fd1b1","admin_user":"fantasy-ke","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","labels":"gitalk","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"f5ce9a778af6d8c5e6308fc5fb343d26"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>


<script src="/js/minigrid.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script data-pjax type="text/javascript">
var album = document.querySelector(".album");
if (album) {
  // 相册列表 JSON 数据
  var imgDataPath = album.getAttribute('json-src');
  // 照片存储路径
  var imgPath = album.getAttribute('photo-src');
  // 最多显示数量
  var imgMaxNum = 50;
  // 获取窗口大小以决定图片宽度
  var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var imageWidth;

  if (windowWidth < 768) {
    imageWidth = 145; // 移动端图片宽度
  } else {
    imageWidth = 235;
  }

  // 生成相册
  var linkDataPath = imgDataPath;
  var photo = {
    page: 1,
    offset: imgMaxNum,
    init: function () {
      var that = this;
      $.getJSON(linkDataPath, function (data) {
          that.render(that.page, data);
      });
    },
    render: function (page, data) {
      var begin = (page - 1) * this.offset;
      var end = page * this.offset;
      if (begin >= data.length) return;
      var imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
      for (var i = begin; i < end && i < data.length; i++) {
        imgNameWithPattern = data[i].path;
        imgName = data[i].name;
        imageSize = data[i].size;
        li += '<div class="card" style="width:' + imageWidth + 'px" >';
        li += '<div class="album-photo" style="height:' + imageWidth + 'px" >';
        li += '<a class="fancybox fancybox.image" href="' + imgPath + imgNameWithPattern + '" itemscope="" itemtype="http://schema.org/ImageObject" itemprop="url" data-fancybox="group" rel="group" data-caption="' + imgName + '" title="' +  imgName + '">';
        li += '<img data-src="' + imgPath + imgNameWithPattern + '" src="' + imgPath + imgNameWithPattern + '" alt="' +  imgName + '" data-loaded="true">';
        li += '</a>';
        li += '</div>';
        li += '</div>';
      }
      album.insertAdjacentHTML('beforeend', li);
      this.minigrid();
    },
    minigrid: function () {
      var grid = new Minigrid({
        container: '.album',
        item: '.card',
        gutter: 12
      });
      grid.mount();
      window.addEventListener('resize', function () {
        grid.mount();
      });
    }
  };
  photo.init();
}
</script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":170,"height":330},"mobile":{"show":true},"react":{"opacity":0.7},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
